﻿/*----------------------------------------------------------------
    // Copyright (C) 2009 江苏华招网
    // 版权所有。 
    // 作者：庄金冬 
    //
    // 文件名：HZ.Data.DAL_Helper.cs
    // 文件功能描述：日志数据访问逻辑
    // 创建标识：周司徒2009-03-09
    //
    // 修改标识：
    // 修改描述：
    //
    // 修改标识：
    // 修改描述：
    //
    // 审核人：
    // 审核时间：
    // 审核说明：
 //----------------------------------------------------------------*/
using System;
using System.Collections.Generic;

using System.Text;
using HZ.DrugTrade.Model;
using System.Data;
using System.Data.SqlClient;
using HZ.Common;

namespace HZ.DrugTrade.DAL
{
    /// <summary>
    /// 日志数据访问逻辑
    /// </summary>
    public class DAL_Log
    {
        /// <summary>
        /// 条件查询日志[OrgID必须赋值]
        /// </summary>
        /// <remarks></remarks>
        /// <param name="user">查询用实体[Operator,OperateMessage,OperateTimeFrom,OperateTimeTo]</param>
        /// <param name="currentPage">当前页</param>
        /// <param name="pageSize">每页显示记录数</param>
        /// <param name="totalRecord">（输出参数）总记录数目</param>
        /// <returns></returns>
        public static IList<KeyOperationLog> GetKeyOperationLogByWhere(KeyOperationLog operationLog, int currentPage, int pageSize, out int totalRecord)
        {
            string sql_temp = @"SELECT * FROM TB_KeyOperationLog WHERE 1=1 ";

            StringBuilder sbWhere = new StringBuilder(sql_temp);

            if (operationLog != null)
            {
                if (!string.IsNullOrEmpty(operationLog.OperationLogSerialID))
                {
                    sbWhere.AppendFormat(" AND  OperationLogSerialID LIKE '%{0}%'", operationLog.OperationLogSerialID.Trim()); // 增加UserID查询条件
                }
                if (!string.IsNullOrEmpty(operationLog.Operator))
                {
                    sbWhere.AppendFormat(" AND  Operator LIKE '%{0}%'", operationLog.Operator.Trim()); // 增加UserName查询条件
                }
                if (!string.IsNullOrEmpty(operationLog.OrgID))
                {
                    //sbWhere.AppendFormat(" AND  OrgID LIKE '%{0}%'", operationLog.OrgID.Trim()); // 增加OrgID查询条件    //updated by lhl 2009-08-28
                    sbWhere.AppendFormat(" AND {0}",operationLog.OrgID);
                }
                if (!string.IsNullOrEmpty(operationLog.OperateMessage))
                {
                    sbWhere.AppendFormat(" AND  OperateMessage LIKE '%{0}%'", operationLog.OperateMessage.Trim()); // 增加RoleID查询条件
                }
                if (operationLog.OperateTimeFrom != null)
                {
                    sbWhere.AppendFormat(" AND  OperateTime >= to_date('{0}' ,'yyyy-mm-dd hh24:Mi:ss')", operationLog.OperateTimeFrom.ToString()); // 增加OperateTimeFrom查询条件
                }

                if (operationLog.OperateTimeTo != null)
                {
                    sbWhere.AppendFormat(" AND  OperateTime <= to_date('{0}' ,'yyyy-mm-dd hh24:Mi:ss')", operationLog.OperateTimeTo.ToString());  // 增加OperateTimeTo查询条件
                }

            }

            sbWhere.Append(" ORDER BY OperateTime DESC  ");
            IList<KeyOperationLog> list;
            //值得检讨的代码 
            //totalRecord = HZ.Data.DbHelperFactory.Create().ExecuteTable(sbWhere.ToString()).Rows.Count;
            HZ.Data.PageDescribe pdes = new HZ.Data.PageDescribe(sbWhere.ToString(), pageSize);
            pdes.CurrentPage = currentPage;
            DataTable dt = pdes.GetSpecailPage();
            totalRecord = pdes.TotalCount;
            //list = HZ.Data.DAL_Helper.GetKeyOperationLogsFromDataTable(dt);
            list = HZ.Data.DAL_Helper.CommonFillList<KeyOperationLog>(dt);
            return list;
        }

        /// <summary>
        /// 添加日志(0,添加失败;1,添加成功;)
        /// </summary>
        /// <param name="user">日志对象（每个日志的Operator，OrgID必须赋值）</param>
        /// <returns>(0,添加失败;1,添加成功;)</returns>
        public static int AddKeyOperationLog(KeyOperationLog keyOperationLog)
        {
            int retValue = 0;
            try
            {
                SqlParameter[] paras = new SqlParameter[5];

                //TB_KeyOperationLog【OperationLogSerialID,Operator,OperateMessage,OrgID】
                //日志编号
                paras[0] = new SqlParameter("@in_OperationLogSerialID", string.IsNullOrEmpty(keyOperationLog.OperationLogSerialID) ? "" : keyOperationLog.OperationLogSerialID.Trim());
                //操作人
                paras[1] = new SqlParameter("@in_Operator", string.IsNullOrEmpty(keyOperationLog.Operator) ? "" : keyOperationLog.Operator.Trim());
                //操作信息
                paras[2] = new SqlParameter("@in_OperateMessage", string.IsNullOrEmpty(keyOperationLog.OperateMessage) ? "" : keyOperationLog.OperateMessage.Trim());

                if (string.IsNullOrEmpty(keyOperationLog.OrgID))
                {
                    new Exception("机构编号相关信息为空，添加日志信息必须提供机构编号信息，故系统阻止继续操作！");
                }
                if (string.IsNullOrEmpty(keyOperationLog.Operator))
                {
                    new Exception("操作人相关信息为空，添加日志信息必须提供机构编号信息，故系统阻止继续操作！");
                }
                //操作机构
                paras[3] = new SqlParameter("@in_OrgID", string.IsNullOrEmpty(keyOperationLog.OrgID) ? "" : keyOperationLog.OrgID.Trim());

                //输入参数处理结果(0,添加失败;1,添加成功;)
                paras[4] = new SqlParameter("@out_success", SqlDbType.Int);
                paras[4].Direction = ParameterDirection.Output;
                //执行存储过程
                HZ.Data.DbHelperFactory.Create().ExecuteNonQuery(CommandType.StoredProcedure, "usp_KeyOperationLog_Add", paras);

                retValue = int.Parse(paras[4].Value.ToString());
            }
            catch (Exception ex)
            {
                //执行不成功，记录日志 
                log4netManager.LogDebug(typeof(DAL_Role).FullName, "添加操作日志不成功", ex);
            }

            return retValue;
        }

    }
}
